from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
Schrödinger had many pets it seems. This worm exhibits quantum behavior that’s fun to examine.
I found this exercise in a book named “Quantum Computing for Quantum Curious”
Create a worm in a superposition state of alive and dead. Let q[0] correspond to the bit on the far right.
def create_circuit(n, qreg_q, creg_c):
= QuantumCircuit(qreg_q, creg_c)
circuit
for _ in range(n):
circuit.x(qreg_q[_])0])
circuit.h(qreg_q[return circuit
= 5
n = QuantumRegister(n, 'q')
qreg_q = ClassicalRegister(n, 'c')
creg_c = create_circuit(n, qreg_q, creg_c ) circuit
The first qubit is in superposition as seen in the bloch spehere
from qiskit.quantum_info import Statevector
= Statevector(circuit)
state plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)'mpl') circuit.draw(
from qiskit import Aer, execute
= Aer.get_backend('qasm_simulator')
simulator = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit) counts
counts
{'11110': 501, '11111': 523}
from qiskit.visualization import plot_histogram, plot_bloch_multivector
plot_histogram(counts)
Modify the circuit so that the worm is first put in a superposition state and then brought to life
= create_circuit(n, qreg_q, creg_c )
circuit 0]) circuit.h(qreg_q[
<qiskit.circuit.instructionset.InstructionSet at 0x7f7d2c118cd0>
= Statevector(circuit)
state plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)'mpl') circuit.draw(
= execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
counts plot_histogram(counts)
Modify the circuit so that the worm in a superposition state becomes definitely dead
= create_circuit(n, qreg_q, creg_c )
circuit 0])
circuit.h(qreg_q[4]) circuit.x(qreg_q[
<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0d627940>
= Statevector(circuit)
state plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)'mpl') circuit.draw(
= execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
counts plot_histogram(counts)
The worm is next to a hungry bird, such that the worm is either alive or chomped to pieces as figure above
Create a circuit that produces a worm in a superposition state of alive and very dead.
= create_circuit(n, qreg_q, creg_c )
circuit 0])
circuit.x(qreg_q[2])
circuit.x(qreg_q[0, 2) circuit.cx(
<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0f1b7580>
= Statevector(circuit)
state plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)'mpl') circuit.draw(
= execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
counts plot_histogram(counts)
# Do not run, just a test to see where x H x will produce the same result as x x H
from qiskit.visualization import array_to_latex
= QuantumCircuit(2)
crk 0)
crk.x(0)
crk.x(0) crk.h(
<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0fb73970>
= Aer.get_backend('aer_simulator')
usim
crk.save_unitary()= usim.run(crk).result().get_unitary() unitary
array_to_latex(unitary)
$$
\[\begin{bmatrix} \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0 & 0 \\ \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} & 0 & 0 \\ 0 & 0 & \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \\ 0 & 0 & \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} \\ \end{bmatrix}\]$$
# and it does
# test ends
Modify the circuit so that the worm in a superposition state becomes either definitely dead or definitely alive
= create_circuit(n, qreg_q, creg_c )
circuit 0])
circuit.x(qreg_q[2])
circuit.x(qreg_q[0, 2)
circuit.cx(0, 2)
circuit.cx(0]) circuit.h(qreg_q[
<qiskit.circuit.instructionset.InstructionSet at 0x7f7d2aef6490>
= Statevector(circuit)
state plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)'mpl') circuit.draw(
= execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
counts plot_histogram(counts)